package com.tfy.liquor.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.UUID;

public class CodeUtils {
    private final static Logger logger = LoggerFactory.getLogger(CodeUtils.class);

    /**
     * 将数据进行MD5加密
     *
     * @param data
     * @param salt
     * @return
     */
    public static String md5Hex(String data, String salt) {
        if (StringUtils.isBlank(salt)) {
            salt = data.hashCode() + "";
        }
        return DigestUtils.md5Hex(salt + DigestUtils.md5Hex(data));
    }

    /**
     * 将数据进行sha512加密
     *
     * @param data
     * @param salt
     * @return
     */
    public static String sha512Hex(String data, String salt) {
        if (StringUtils.isBlank(salt)) {
            salt = data.hashCode() + "";
        }
        return DigestUtils.sha512Hex(salt + DigestUtils.sha512Hex(data));
    }

    /**
     * 生成uuid，作为用户注册时的盐
     */
    public static String generateSalt() {
        return StringUtils.replace(UUID.randomUUID().toString(), "-", "");
    }

    /**
     * 订单号生成工具
     */
    public static String getOrderId() {
        String time = DateUtil.getMilliSecond();
        String random = RandomStringUtils.randomNumeric(4);
        return time + random;
    }

    /**
     * base64编码工具
     *
     * @param text
     */
    public static String base64Encode(String text) {
        final Base64.Encoder encoder = Base64.getEncoder();
        byte[] textByte = new byte[0];
        try {
            textByte = text.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            logger.error("base64编码错误：错误数据为：" + text);
            e.printStackTrace();
        }
        //编码
        return encoder.encodeToString(textByte);
    }

    /**
     * base64解码工具
     *
     * @param text
     */
    public static String base64Decoder(String text) {
        final Base64.Decoder decoder = Base64.getDecoder();
        //解码
        try {
            return new String(decoder.decode(text), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            logger.error("base64解码错误：错误数据为：" + text);
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        List list = new ArrayList();
        long begin = System.currentTimeMillis();
//        SnowFlake idWorker = new SnowFlake(0, 0);
//        for (int i = 0; i < 1000000; i++) {
////            list.add(getOrderId());
////            list.add(idWorker.nextId());
//            System.err.println(idWorker.nextId());
//        }
        String test = base64Encode("以上的程式码看起来又比早期用sun.mis c套件还要更精简，效能实际执行起来也快了不少。缺点是需要引用Apache Commons Codec，很麻烦。");
        System.err.println(test);
        String test1 = base64Decoder(test);

        long end = System.currentTimeMillis();
        System.err.println("消耗时间：" + (end - begin) + "毫秒");
    }

}